package com.funShopping.config.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.Serializable;

/**
 * @program: funShopping
 * @description: 全局响应实体
 * @author: talented_zhang
 * @create: 2024-03-30 16:31
 **/
@Data
public class ResultResponse implements Serializable {
    private static final long serialVersionUID = 8329562780042069741L;

    private Integer code;//响应码
    private String message;//响应信息
    private Object data;//响应数据

    private static ResultResponse resultResponse =new ResultResponse();

    /**
    * @Description: 成功响应没有响应数据
    * @Param: 响应信息
    * @return:
    * @Author: talented_zhang
    * @Date: 2024/3/30
    */

    @ResponseStatus(HttpStatus.OK)
    public static ResultResponse success(String message){
        resultResponse.setMessage(message);
        return resultResponse;
    }

    /**
    * @Description: 自定义响应信息+响应数据
    * @Param:
    * @return:
    * @Author: talented_zhang
    * @Date: 2024/3/30
    */

    @ResponseStatus(HttpStatus.OK)
    public static ResultResponse success(String message,Object data){
        resultResponse.setMessage(message);
        resultResponse.setData(data);
        return resultResponse;
    }

    /** 
    * @Description: 响应成功+响应数据
    * @Param: 
    * @return: 
    * @Author: talented_zhang
    * @Date: 2024/3/30
    */
    @ResponseStatus(HttpStatus.OK)
    public static ResultResponse success(Object data){
        resultResponse.setMessage(CommonMessage.SUCCESS_Message.getMessage());
        resultResponse.setData(data);
        return resultResponse;
    }


    @AllArgsConstructor
    @Getter
    public enum CommonMessage{

        SUCCESS_Message("交易成功"),
        FAIL_Message("交易失败"),
        INTERNAL_SERVER_ERROR_Message("系统异常{}"),
        FORBIDDEN_Message("无访问权限，请联系管理员授予权限"),
        CUSTOM_Message("自定义异常------》");
        
        private String message;

    }
    
    /** 
    * @Description: 自定义响应失败+响应码
    * @Param: 
    * @return: 
    * @Author: talented_zhang
    * @Date: 2024/3/30
    */
    
    public static ResultResponse fail(Integer code,String message){
        resultResponse.setMessage(message);
        resultResponse.setCode(code);
        return resultResponse;
    }
    
    /** 
    * @Description: 响应失败
    * @Param: 
    * @return: 
    * @Author: talented_zhang
    * @Date: 2024/3/30
    */
    
    public static ResultResponse fail(Integer code){
        resultResponse.setMessage(CommonMessage.FAIL_Message.getMessage());
        resultResponse.setCode(code);
        return resultResponse;
    }

    public static ResultResponse fail(Integer code,String message,Object data){
        resultResponse.setCode(code);
        resultResponse.setMessage(message);
        resultResponse.setData(data);
        return resultResponse;
    }

    public static ResultResponse systemException(Object data){
        resultResponse.setData(data);
        resultResponse.setMessage(CommonMessage.INTERNAL_SERVER_ERROR_Message.getMessage());
        return resultResponse;
    }

    public static ResultResponse forbiddenException(Object data){
        resultResponse.setMessage(CommonMessage.FORBIDDEN_Message.getMessage());
        resultResponse.setData(data);
        return resultResponse;
    }

    public static ResultResponse customException(Integer code,String message){
        resultResponse.setMessage(message);
        resultResponse.setCode(code);
        return resultResponse;
    }
}
